<template>
    <div class="policy">
        <!-- search -->
        <div class="search">
            <input type="text" @focus="inputFocus" @blur="inputBlur" v-model="searchInput" class="searchInput" />
            <i class="search-icon" v-show="searchShow"></i>
            <span class="searchPlaceHolder" v-show="searchShow">搜索</span>
        </div>
        <mTitle :title="title"></mTitle>
        <scroll class="policy-wrapper" :data="hotspotList" :pullup="pullup" @scrollToEnd="searchMore" v-show="!searchInput">
            <div>
                <ul class="policy-item">
                    <router-link :to="'/detail/' + item.id" class="policy-list" v-for="(item, index) in hotspotList" :key="index" tag="li">
                        <div class="policy-list-title">{{item.title}}</div>
                        <div class="policy-list-time">{{item.createtime}}</div>
                    </router-link>
                    <loading v-show="!hasMore"></loading>
                    <li class="bottom-text" v-show="endMore">已加载全部数据</li>
                </ul>
            </div>
            <!-- 请求数据loading -->
            <div class="loading-wrapper" v-show="!hotspotList.length">
                <loading></loading>
            </div>
        </scroll>
        <!-- 搜索列表 -->
        <scroll class="policy-wrapper" :data="list" v-show="list.length">
            <div>
                <ul class="policy-item">
                    <router-link :to="'/detail/' + item.id" class="policy-list" v-for="(item, index) in list" :key="index" tag="li">
                        <div class="policy-list-title">{{item.title}}</div>
                        <div class="policy-list-time">{{item.createtime}}</div>
                    </router-link>
                </ul>
            </div>
        </scroll>
        <!-- 底部 -->
        <div class="info-fixed">
            <router-link to="/info" class="info-fixed-item" replace>
                <i class="icon icon-item1"></i>
                <span class="desc">本馆介绍</span>
            </router-link>
            <router-link to="/tissue" class="info-fixed-item" replace>
                <i class="icon icon-item2"></i>
                <span class="desc">组织机构</span>
            </router-link>
            <router-link to="/policy" class="info-fixed-item active" replace>
                <i class="icon icon-item3"></i>
                <span class="desc">政策法规</span>
            </router-link>
            <router-link to="/party" class="info-fixed-item" replace>
                <i class="icon icon-item4"></i>
                <span class="desc">党建工作</span>
            </router-link>
        </div>
        <div class="no-result" v-show="!list.length && searchInput">暂无搜索结果</div> 
    </div>
</template>

<script>
    import axios from 'axios'
    import mTitle from 'base/title/title'
    import Scroll from 'base/scroll/scroll'
    import Loading from 'base/loading/loading'
    export default {
        data () {
            return {
                searchShow: true,
                searchInput: "",
                title: '政策法规',
                pullup: true,
                hotspotList: [],
                searchList: [],
                list: [],
                hasMore: true,
                endMore: false,
                num: 1,
                size: 20,
                total: 0,
            }
        },
        methods: {
            // 获取焦点
            inputFocus () {
                this.searchShow = false
                if (this.searchList.length > 0) {
                    return
                } else {
                    axios.get('/api/index.php?g=Api&m=Guanwang&a=zhengce&page='+ this.num +'&size=' + this.total +'').then((res) => {
                        res = res.data
                        this.searchList = res.list
                    })
                }
            },
            // 失去焦点
            inputBlur () {
                if (this.searchInput === "") {
                    this.searchShow = true
                }
            },
            // 请求数据
            getPolicyInfo () {
                axios.get('/api/index.php?g=Api&m=Guanwang&a=zhengce&page='+ this.num +'&size=' + this.size +'').then((res) => {
                    res = res.data
                    this.total = res.total
                    this.hasMore = false
                    this.hotspotList = res.list
                    if (this.total < 20) {
                        this.hasMore = true
                        this.endMore = true
                        return
                    }
                })
            },
            // 请求全部数据
            searchMore () {
                if (this.total < 20) {
                    this.hasMore = true
                    this.endMore = true
                    return
                }
                this.num = this.num+1
                if ((this.num-1) * this.size <= this.total) {
                    axios.get('/api/index.php?g=Api&m=Guanwang&a=zhengce&page='+ this.num +'&size=' + this.size +'').then((res) => {
                        let data = res.data.list
                        if (!data.length) {
                            this.hasMore = true
                        }
                        this.hotspotList = this.hotspotList.concat(data)
                    })
                } else {
                    this.hasMore = true
                    this.endMore = true
                } 
            }
        },
        components: {mTitle,Scroll,Loading},
        watch: {
            // 监听input变化进行搜索判断
            searchInput () {
                this.list = []
                let result = []
                if(this.searchInput === "") {
                    result = []
                    this.list = []
                    return
                }
                for (let i in this.searchList) {
                    let value = this.searchList[i].title
                    if(value.indexOf(this.searchInput) > -1) {
                        result.push(i)
                    }
                }
                for(let k in result) {
                    this.list = this.list.concat(this.searchList[result[k]])
                }
            }
        },
        created () {
            this.getPolicyInfo()
        }
    }
</script>

<style lang="stylus" scoped>
    @import '~common/stylus/mixins.styl'
    
    .policy
        width: 100%
        height: 100%
        position: relative
        overflow: hidden
        background: #fff
        .search
            width: 100%
            height: 44px
            background: #efeff4
            padding: 0 15px
            box-sizing: border-box
            -webkit-box-sizing: border-box
            position: relative
            overflow: hidden
            .searchInput
                width: 100%
                height: 32px
                background: #fff
                margin-top: 6px
                border: none
                border-radius: 0
                padding: 0 10px
                box-sizing: border-box
                -webkit-box-sizing: border-box
            .search-icon
                position: absolute
                background: url('./search-icon.png') no-repeat
                width: 13px
                height: 13px
                background-size: 100% 100%
                top: 16px
                left: 50%
                margin-left: -15px
            .searchPlaceHolder
                font-size: 14px
                color: #979797
                position: absolute
                top: 17px
                left: 51%
        .policy-wrapper
            position: absolute
            top: 86px
            bottom: 54px
            width: 100%
            overflow: hidden
            .loading-wrapper
                position: absolute
                top: 50%
                transform: translateY(-50%)
                width: 100%
            .policy-item
                padding: 0 15px
                box-sizing: border-box
                width: 100%
                .policy-list
                    width: 100%
                    height: 44px
                    line-height: 44px
                    border-bottom: 1px solid #eee
                    font-size: 0
                    display: flex
                    .policy-list-title
                        display: inline-block
                        font-size: 14px
                        flex: 1
                        ellipsis()
                    .policy-list-time
                        display: inline-block
                        font-size: 12px
                        color: #888
                        flex: 0 0 70px
                        width: 70px
                        text-align: right
                .bottom-text
                    width: 100%
                    height: 40px
                    line-height: 40px
                    font-size: 14px
                    text-align: center
                    color: #ddd
                    justify-content: center
                    padding: 0
        .no-result
            padding: 0 15px
            line-height: 30px
            font-size: 16px
            color: #888
        .info-fixed
            position: fixed
            bottom: 0
            height: 54px
            left: 0
            width: 100%
            background: #fff
            box-sizing: border-box
            align-items: center
            padding: 0 15px
            display: flex
            border-top: 1px solid rgba(7,17,27,.1)
            .info-fixed-item
                flex: 1
                overflow: hidden
                &.active
                    .icon-item1
                        background: url('./info-icon1-active.png') no-repeat
                        background-size: 100% 100%
                    .desc
                        color: #0094d4
                    .icon-item2
                        background: url('./info-icon2-active.png') no-repeat
                        background-size: 100% 100%
                    .desc
                        color: #0094d4
                    .icon-item3
                        background: url('./info-icon3-active.png') no-repeat
                        background-size: 100% 100%
                    .desc
                        color: #0094d4
                    .icon-item4
                        background: url('./info-icon4-active.png') no-repeat
                        background-size: 100% 100%
                    .desc
                        color: #0094d4
                .icon-item1
                    background: url('./info-icon1.png') no-repeat
                    width: 20px
                    height: 20px
                    background-size: 100% 100%
                    display: block
                    margin: 0 auto
                .desc
                    color: #a1a1a1
                    text-align: center
                    font-size: 12px
                    display: block
                    margin-top: 4px
                .icon-item2
                    background: url('./info-icon2.png') no-repeat
                    width: 21px
                    height: 20px
                    background-size: 100% 100%
                    display: block
                    margin: 0 auto
                .desc
                    color: #a1a1a1
                    text-align: center
                    font-size: 12px
                    display: block
                    margin-top: 4px
                .icon-item3
                    background: url('./info-icon3.png') no-repeat
                    width: 21px
                    height: 20px
                    background-size: 100% 100%
                    display: block
                    margin: 0 auto
                .desc
                    color: #a1a1a1
                    text-align: center
                    font-size: 12px
                    display: block
                    margin-top: 4px
                .icon-item4
                    background: url('./info-icon4.png') no-repeat
                    width: 21px
                    height: 20px
                    background-size: 100% 100%
                    display: block
                    margin: 0 auto
                .desc
                    color: #a1a1a1
                    text-align: center
                    font-size: 12px
                    display: block
                    margin-top: 4px
</style>